﻿using System;
using System.Collections.Generic;
using System.Text;
using Chloe;
using lingopen.Application.System;
using lingopen.Entity.BLE;
using lingopen.Entity.System;

namespace lingopen.Application.BLE.Implements
{
    public class BLEMonitorAppService : AdminAppService, IBLEMonitorAppService
    {
        public PagedData<Monitor_Battery_Model> GetMonitor_Battery_Model(Pagination pagination, string keyword)
        {
            try
            {
                var config = DbContext.Query<Sys_Config>().FirstOrDefault(p => p.Code == "BatteryLow");
                int ilow = Convert.ToInt32(config?.Value);
                var query = DbContext.Query<BLE_Alarm_Battery>()
                .WhereIfNotNullOrEmpty(keyword, a => a.MAC == keyword || a.EmployeeName.Contains(keyword) || a.EmpLocation.Contains(keyword) || a.IP.Contains(keyword))
                .OrderByDesc(a => a.SyncTime)
                .Select(a => new Monitor_Battery_Model
                {
                    CreationTime = a.SyncTime,
                    Emp_MAC = a.MAC,
                    Emp_Name = a.EmployeeName,
                    Duty_Code = a.IP,
                    Duty_Name = a.EmpLocation,
                    Battery = a.Battery,
                    BatteryLow = ilow
                });
                //    var query = DbContext.JoinQuery<BLE_Alarm_Battery, Sys_Config>((a, b) => new object[]
                //{
                //   JoinType.LeftJoin,a.IsDeleted==false,
                //   JoinType.LeftJoin,b.Code== "BatteryLow"&&b.Value.IsNotNull()
                //})
                //.Select((a, b) => new Monitor_Battery_Model
                //{
                //    CreationTime = a.CreationTime,
                //    Emp_MAC = a.MAC,
                //    Emp_Name = a.EmployeeName,
                //    Duty_Code = a.IP,
                //    Duty_Name = a.EmpLocation,
                //    Battery = a.Battery,
                //    BatteryLow = int.Parse(b.Value)
                //})
                // .WhereIfNotNullOrEmpty(keyword, a => a.Emp_MAC == keyword || a.Emp_Name.Contains(keyword) || a.Duty_Code.Contains(keyword) || a.Duty_Name.Contains(keyword))
                // .OrderByDesc(a => a.CreationTime);

                PagedData<Monitor_Battery_Model> ret = query.TakePageData(pagination);
                return ret;
            }
            catch (Exception ex)
            {
                (ex + "").Log();
                return null;
            }
        }

        public PagedData<Monitor_Leave_Model> GetMonitor_Leave_Model(Pagination pagination, string keyword)
        {
            var query = DbContext.Query<BLE_Alarm_Leave>().FilterDeleted() 
                .WhereIfNotNullOrEmpty(keyword, a => a.MAC == keyword || a.EmployeeName.Contains(keyword) || a.IP.Contains(keyword) || a.EmpLocation.Contains(keyword))
                 .OrderByDesc(a => a.SyncTime)
             .Select(a => new Monitor_Leave_Model
             {
                 CreationTime = a.SyncTime,
                 Emp_MAC = a.MAC,
                 Emp_Name = a.EmployeeName,
                 Duty_Code = a.IP,
                 Duty_Name = a.EmpLocation
             });
            //.WhereIfNotNullOrEmpty(keyword, a => a.Emp_MAC == keyword || a.Emp_Name.Contains(keyword) || a.Duty_Code.Contains(keyword) || a.Duty_Name.Contains(keyword))
            //.OrderByDesc(a => a.CreationTime);


            PagedData<Monitor_Leave_Model> ret = query.TakePageData(pagination);
            return ret;
        }

        public PagedData<Monitor_Sleep_Model> GetMonitor_Sleep_Model(Pagination pagination, string keyword)
        {
            var query = DbContext.Query<BLE_Alarm_Sleep>().FilterDeleted() 
               .WhereIfNotNullOrEmpty(keyword, a => a.MAC == keyword || a.EmployeeName.Contains(keyword) || a.IP.Contains(keyword) || a.EmpLocation.Contains(keyword))
                .OrderByDesc(a => a.SyncTime)
            .Select(a => new Monitor_Sleep_Model
            {
                CreationTime = a.SyncTime,
                Emp_MAC = a.MAC,
                Emp_Name = a.EmployeeName,
                Duty_Code = a.IP,
                Duty_Name = a.EmpLocation
            });
            //.WhereIfNotNullOrEmpty(keyword, a => a.Emp_MAC == keyword || a.Emp_Name.Contains(keyword) || a.Duty_Code.Contains(keyword) || a.Duty_Name.Contains(keyword))
            //.OrderByDesc(a => a.CreationTime);


            PagedData<Monitor_Sleep_Model> ret = query.TakePageData(pagination);
            return ret;
        }

        public PagedData<Monitor_Transfer_Model> GetMonitor_Transfer_Model(Pagination pagination, string keyword)
        {
            var query = DbContext.Query<BLE_Transfer_Api>().FilterDeleted()
               .WhereIfNotNullOrEmpty(keyword, a => a.MAC == keyword || a.EmployeeName.Contains(keyword) || a.IP.Contains(keyword) || a.EmpLocation.Contains(keyword))
                .OrderByDesc(a => a.SyncTime)
                .Select(a => new Monitor_Transfer_Model
                {
                    Rssi = a.Rssi,
                    CreationTime = a.SyncTime,
                    Emp_MAC = a.MAC,
                    Emp_Name = a.EmployeeName,
                    Duty_Code = a.IP,
                    Duty_Name = a.EmpLocation

                });
            //var query = DbContext.JoinQuery<BLE_Transfer_Api, BLE_Emp_Card, BLE_Duty>((a, b, c) => new object[]
            //{
            //   JoinType.LeftJoin,a.MAC==b.MAC&&b.IsDeleted==false,
            //   JoinType.LeftJoin,a.IP==c.Code&&c.IsDeleted==false
            //})
            //.Select((a, b, c) => new Monitor_Transfer_Model
            //{
            //    Rssi = a.Rssi,
            //    CreationTime = a.CreationTime,
            //    Emp_MAC = b.MAC,
            //    Emp_Name = b.Name,
            //    Duty_Code = c.Code,
            //    Duty_Name = c.Name,

            //})
            //.WhereIfNotNullOrEmpty(keyword, a => a.Emp_MAC == keyword || a.Emp_Name.Contains(keyword) || a.Duty_Code.Contains(keyword) || a.Duty_Name.Contains(keyword))
            //.OrderByDesc(a => a.CreationTime);
            PagedData<Monitor_Transfer_Model> ret = query.TakePageData(pagination);
            return ret;
        }
    }
}
